<?php
/**
 * 
 * Created on 2014-12-8
 * Create by LDD
 */

namespace haibao\cms\business;

use \haibao\cms\model\filter\Event as EventFilter;
use \haibao\frame\data\query\Condition;
use \haibao\cms\business\Tag as TagBussiness;

class Event extends \haibao\cms\business\Base{
	
	/**
	 * 检查事件名称是否存在
	 */
	public static function hasSameEventName($eventName,$eventAlias,$eventId = null){
		if(!$eventName){
			throw new BusinessException("事件名称不能为空",self::DATA_EMPTY_NAME);
		}
		$eventFilter = new EventFilter();
		if($eventId){
			$eventFilter->where(EventFilter::CONDITION_FIELD_NAME_ID, Condition::CONDITION_NOTEQUAL, $eventId);
		}
		if(!$eventAlias){
			$eventFilter->where(EventFilter::CONDITION_FIELD_NAME_EVENT_NAME, Condition::CONDITION_EQUAL, $eventName);
		}else{
			$eventFilter->where(EventFilter::CONDITION_FIELD_NAME_ID, Condition::CONDITION_OR,array(
					new Condition(EventFilter::CONDITION_FIELD_NAME_EVENT_NAME, Condition::CONDITION_EQUAL, $eventName),
					new Condition(EventFilter::CONDITION_FIELD_NAME_ALIAS, Condition::CONDITION_LIKE, '%'.$eventAlias.'%'),
					new Condition(EventFilter::CONDITION_FIELD_NAME_EVENT_NAME, Condition::CONDITION_LIKE, '%'.$eventAlias.'%')
			));
		}
     	$eventData = new \haibao\cms\data\Event();
		$result = $eventData->getOne($eventFilter);
		
		if($result){
			return $result->Id;
		}else{
			return false;
		}
	}
	/**
	 * @param \haibao\cms\model\data\EventDefine $eventModel
	 */
	public static function addEvent($eventModel){
		self::checkOperation(self::FUNC_CODE_EVENT_ADD);
		
		if(!$eventModel || !($eventModel instanceof \haibao\cms\model\data\EventDefine)){
			throw new BusinessException('事件不能为空',self::DATA_EMPTY);
		}
		if(!$eventModel->EventName){
			throw new BusinessException('事件不能为空',self::DATA_EMPTY_NAME);
		}
		if(self::hasSameEventName($eventModel->EventName,$eventModel->Alias)){
			throw new BusinessException('事件名称或别名已存在',self::DATA_ERROR_NAME_EXIST);
		}
// 		if(!$tagIds = $eventModel->TagIds){
// 			throw new BusinessException('标签不能为空',self::DATA_EMPTY_EVNET_TAG);
// 		}
		if($eventModel->StartDate && $eventModel->EndDate && ($eventModel->StartDate > $eventModel->EndDate)){
			throw new BusinessException('事件开始日期要小于结束日期',self::DATA_ERROR);
		}
		if($eventModel->StartTime && $eventModel->EndTime && ($eventModel->StartTime > $eventModel->EndTime)){
			throw new BusinessException('事件开始时间要小于结束时间',self::DATA_ERROR);
		}

		$eventModel->Status = \haibao\cms\model\data\EventDefine::STATUS_ENABLE;
		$eventModel->CreateBy = self::getCurrentUser()->Id;
		$eventModel->CreateTime = new \DateTime();
		
		self::setEventTag($eventModel);
		self::setEventKeywords($eventModel);
		self::setEventCategory($eventModel);
	
		$eventData = new \haibao\cms\data\Event();
		$eventData->add($eventModel);
		
		self::saveOperateHistory(self::FUNC_CODE_EVENT_ADD, $eventModel->toArray(false));
	}
	/**
	 * 获取事件组
	 */
	public static function getEventById($eventId){
		
		$data= new \haibao\cms\data\Event();
		$eventData = $data->getEventById($eventId);
		return $eventData;
	}
	/**
	 * 通过事件ids获取事件名
	 */
	public static function getEventNameByIds($eventIds){
		if(!is_array($eventIds)){
			$eventIds = array($eventIds);
		}
		$data= new \haibao\cms\data\Event();
		return $data->getEventNameByIds($eventIds);
	}
	/**
	 *保存事件组
	 * @param \haibao\cms\model\data\EventDefine $eventModel
	 */
	public static function saveEvent($eventModel){
		self::checkOperation(self::FUNC_CODE_EVENT_SAVE);
		
		if(!$eventModel || !($eventModel instanceof \haibao\cms\model\data\EventDefine)){
			throw new BusinessException('事件不能为空',self::DATA_EMPTY);
		}
		if(!$eventModel->Id){
			throw new BusinessException('事件Id不能为空',self::DATA_EMPTY_ID);
		}
		if(!$eventModel->EventName){
			throw new BusinessException('事件不能为空',self::DATA_EMPTY_NAME);
		}
		if(self::hasSameEventName($eventModel->EventName,$eventModel->Alias,$eventModel->Id)){
			throw new BusinessException('事件名称或别名已存在',self::DATA_ERROR_NAME_EXIST);
		}
// 		if(!$tagIds = $eventModel->TagIds){
// 			throw new BusinessException('标签不能为空',self::DATA_EMPTY_EVNET_TAG);
// 		}
		if($eventModel->StartDate && $eventModel->EndDate && ($eventModel->StartDate > $eventModel->EndDate)){
			throw new BusinessException('事件开始日期要小于结束日期',self::DATA_ERROR);
		}
		if($eventModel->StartTime && $eventModel->EndTime && ($eventModel->StartTime > $eventModel->EndTime)){
			throw new BusinessException('事件开始时间要小于结束时间',self::DATA_ERROR);
		}
		
		
		self::setEventTag($eventModel);
		self::setEventKeywords($eventModel);
		self::setEventCategory($eventModel);
		
		$eventData = new \haibao\cms\data\Event();
		$eventData->save($eventModel);	
		
		self::saveOperateHistory(self::FUNC_CODE_EVENT_SAVE, $eventModel->toArray());
	}
	
	/**
	 * 删除事件
	 */
	 public static function deleteEvent($eventId){
		self::checkOperation(self::FUNC_CODE_EVENT_DELETE);
		
		if(!$eventId){
			throw new BusinessException('事件Id不能为空',self::DATA_EMPTY_ID);
		}
		$eventData = new \haibao\cms\data\Event();
		
		$arrSet = array();
		$arrSet['Status']  = \haibao\cms\model\data\EventDefine::STATUS_DISABLE;
		$eventData->delete($eventId,$arrSet);
		
		self::saveOperateHistory(self::FUNC_CODE_EVENT_DELETE, array('Id' =>$eventId));
	}
	/**
	 * @param \haibao\cms\model\filter\Event $eventFilter
	 */
	public static function getEventByFilter($eventFilter){
		$eventData = new \haibao\cms\data\Event();
		return $eventData->getAll($eventFilter);
	}
	/**
	 * @param \haibao\cms\model\data\Event $eventModel
	 */
	public static function setEventTag($eventModel){
		if($eventModel->TagIds){
			$tagParentArr = array();
			$tagModel = new \haibao\cms\data\Tag();
			$eventModel->Tags = array();
			$level = 1;
			foreach($eventModel->TagIds as $key=>$tagId){
				$eventTag = new \haibao\cms\model\data\EventTag();
				$eventTag->TagId = $tagId;
				$eventTag->Level = $level;
				$eventTag->Display = \haibao\cms\model\data\EventTag::IS_DISPLAY;
				if(!in_array($eventTag, $eventModel->Tags)){
					array_push($eventModel->Tags, $eventTag);
					$level++;
				}
				/* foreach($tagModel->getParentsTagArr($tagId) as $key =>$parentTag){
				 $eventTag = new \haibao\cms\model\data\EventTag();
				$eventTag->TagId = $parentTag->Id;
				$eventTag->Level = $level;
				$eventTag->Display = \haibao\cms\model\data\EventTag::IS_NOT_DISPLAY;
				if(!in_array($eventTag, $eventModel->Tags)){
				array_push($eventModel->Tags, $eventTag);
				$level++;
				}
				} */
			}
		}
		return $eventModel;
	}
	/**
	 * @param \haibao\cms\model\data\Event $eventModel
	 */
	public static function setEventKeywords($eventModel){
		if($eventModel->Keywords){
				$keywordArr = explode(',', $eventModel->Keywords);
				$eventModel->Keywords = array();
				foreach($keywordArr as $keyword){
					if($keyword){
						$eventKeywords = new \haibao\cms\model\data\EventKeywords();
						$eventKeywords->KeywordId = $keyword;
						array_push($eventModel->Keywords, $eventKeywords);
					}
				}
			}
			return $eventModel;
		}
		
	public static function getKeywordByName($keyword){
		$eventData = new \haibao\cms\data\Event();
		return $eventData->getKeywordByName($keyword);
	}
	
	public static function setEventCategory($eventModel){
		$eventModel->Categories = array();
		$level = 1;
		if($eventModel->CategoryIds){
			foreach($eventModel->CategoryIds as $key=>$categoryId){
				$eventCategory = new \haibao\cms\model\data\EventCategory();
				$eventCategory->CategoryId = $categoryId;
				$eventCategory->Level = $level;
				if(!in_array($eventCategory, $eventModel->Categories)){
					array_push($eventModel->Categories, $eventCategory);
					$level++;
				}	
			}
		}
	return $eventModel;
	}
}